package com.kingsoft.miot.bridge.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.hibernate.validator.HibernateValidator;

import com.kingsoft.miot.bridge.pojo.manager.ManagerInfo;

/**
 * hibernate 数据校验
 * 
 * @author HUJIANQING
 *
 */
public class ValidationUtils {

	/**
	 * 使用hibernate的注解来进行验证
	 * 
	 */
	private static final Validator validator = Validation.byProvider(HibernateValidator.class).configure()
			.failFast(true).buildValidatorFactory().getValidator();

	/**
	 * 功能描述: <br>
	 * 〈注解验证参数〉
	 *
	 * @param obj
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	public static <T> void validate(T obj) {
		Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
		// 抛出检验异常
		if (constraintViolations.size() > 0) {
			throw new RuntimeException(String.format("参数校验失败:%s", constraintViolations.iterator().next().getMessage()));
		}
	}

	/**
	 * 根据房间状态值返回对应的信息
	 * @param status
	 * @return
	 */
	public static Map<String,Object> getHouseStatus(Integer status){
		Map<String, Object> houseStatus = new HashMap<>();
		if(status == 1){
			houseStatus.put("name", "有客");
			houseStatus.put("value", 1);
			return houseStatus;
		}
		houseStatus.put("name", "空房");
		houseStatus.put("value", 0);
		return houseStatus;
	}
	/**
	 * 根据设备在线
	 * @param status
	 * @return
	 */
	public static Map<String,Object> getOnlineStatus(Integer status){
		Map<String, Object> result = new HashMap<>();
		if(status == 1){
			result.put("name", "在线");
			result.put("value", 1);
			return result;
		}else if(status == 0){
			result.put("name", "离线");
			result.put("value", 0);
			return result;
		}else {
			result.put("name", "报警");
			result.put("value", 2);
			return result;
		}

	}
	/**
	 * 设备状态
	 * @param status
	 * @return
	 */
	public static Map<String,Object> getDeviceStatus(Integer status){
		Map<String, Object> result = new HashMap<>();
		if(status == 1){
			result.put("name", "开");
			result.put("value", 1);
			return result;
		}
		result.put("name", "关");
		result.put("value", 0);
		return result;
	}

	public static void main(String[] args) {
		ManagerInfo managerInfo = new ManagerInfo();
		ValidationUtils.validate(managerInfo);

	}

}
